/**
 * Created by jason on 14-3-11.
 */
$(function () {

    var $window = $("#bg");
    var space = 130; // 活动间距
    var birdLeft; //小鸟的位置

    //水管对象
    function pipe() {
        Pipe = this;

        Pipe.$TopPipe = $("<div>").addClass("TopPipe").attr("pass", '0');
        Pipe.$BottomPipe = $("<div>").addClass("BottomPipe").attr("pass", '0');

        Pipe.create = function () {
            pipeTopHeight = Math.floor(Math.random() * ($window.height()/2)) + 50;
            $window.append(Pipe.$TopPipe.css("height", pipeTopHeight + 'px'));


            pipeBttomHeight = $window.height() - (pipeTopHeight + space);
            $window.append(Pipe.$BottomPipe.css("height", pipeBttomHeight + 'px'));
            return Pipe;
        }

        Pipe.animate = function () {
            Pipe.$TopPipe.animate({'left': '-60px'}, 10000, 'linear');
            Pipe.$BottomPipe.animate({'left': '-60px'}, 10000, 'linear');
            return Pipe;
        }
    }

    //小鸟对象
    function bird() {
        Bird = this;
        Bird.status = "life";
        Bird.$bird = $("<div>");

        var fallTime = 3000;

        Bird.start = function () {
            Bird.$bird.addClass("bird");
            birdLeft = $window.width()/2-20;
            Bird.$bird.css("left",birdLeft);

            Bird.checkKeyDown();
            $window.append(Bird.$bird);
            return Bird;
        }

        Bird.die = function () {
            Bird.$bird.stop();
            Bird.status = "die";
            Bird.flyDown();
            return Bird;
        }


        Bird.flyUp = function () {
            Bird.$bird.css('transform', 'rotate(-20deg)');
            Bird.$bird.stop().animate({bottom: '+=45px'}, 300, 'linear', function () {
                Bird.$bird.css('transform', 'rotate(0deg)');
                Bird.$bird.stop().animate({bottom: '-=45px'}, 300, 'linear', function () {
                    Bird.flyDown();
                });
            });
        }


        Bird.flyDown = function () {
            birdPercent = parseInt(Bird.$bird.css('bottom')) / $window.height();
            totalFallTime = fallTime * birdPercent;
            Bird.$bird.stop().animate({bottom: '0'}, totalFallTime, 'linear');
            Bird.$bird.css('transform', 'rotate(60deg)');
        }

        Bird.checkKeyDown = function () {
            $(document).keydown(function (k) {
                var key = k.which;
                if (key == 32 && Bird.status == "life") {
                    Bird.flyUp();
                }
            });
            return Bird;
        }

    }


    function Score() {
        Score = this;

        Score.$div;

        Score.start = function () {
            Score.$div = $("<div>");
            $window.append(Score.$div.addClass("score").text(0));
        }

        Score.addScore = function () {
            var score = Score.$div.text();
            Score.$div.text(++score);
        }

    }


    function Game() {
        Game = this;
        Game.status = "start";
        Game.bird = new bird();
        Game.score = new Score();


        var threadPipe;
        var threadCheckPipe;
        var threadReferee;

        Game.start = function () {
            Game.bird.start();
            Game.score.start();
            return Game;
        }


        Game.run = function () {
            threadPipe = setInterval(Game.createPipe, 3000);
            threadCheckPipe = setInterval(Game.checkPipes, 50);
            threadReferee = setInterval(Game.Referee, 50);
        }

        Game.createPipe = function () {
            (new pipe()).create().animate();
        }

        Game.checkPipes = function () {
            var $firstTopPipe = $("div.TopPipe:first");
            var $firstBottomPipe = $("div.BottomPipe:first");

            if ($firstTopPipe.length) {
                var Tleft = $firstTopPipe.position();
                if (Tleft.left < -59) {
                    $firstTopPipe.remove();
                }
            }

            if ($firstBottomPipe.length) {
                var Bleft = $firstBottomPipe.position();
                if (Bleft.left < -59) {
                    $firstBottomPipe.remove();
                }
            }
        }

        Game.Referee = function () {
            var birdPosition = Game.bird.$bird.position();
            var $TopPipe = $("div.TopPipe[pass!=1]:first");
            var pipePosition = $TopPipe.position();

            var pipeStar = birdLeft+35;
            var pipeEnd = birdLeft-60;
            //console.log($TopPipe.height(),$TopPipe.height() + space);
          //  if ($TopPipe.length && pipePosition.left < 220 && pipePosition.left > (187 - 60)) {
            if ($TopPipe.length && pipePosition.left < pipeStar && pipePosition.left > (pipeEnd)) {
                if (birdPosition.top <= $TopPipe.height() || birdPosition.top >= $TopPipe.height()-14 + space) {
                    Game.lost();
                }
            }

            if ($TopPipe.length && Game.status == 'start') {
                if (pipePosition.left + 60 < birdLeft) {
                    $TopPipe.attr('pass', 1);
                    Game.score.addScore();
                }
            }
        }

        Game.lost = function () {
            Game.status = 'stop';
            $("div.TopPipe,div.BottomPipe").stop();
            threadPipe = clearInterval(threadPipe);
            threadCheckPipe = clearInterval(threadCheckPipe);
            threadReferee = clearInterval(threadReferee);
            Game.bird.die();
        }
    }


    (new Game()).start().run();
});



